package org.mspring.platform.config;

import java.util.Map;

import org.mspring.platform.cache.lru.LruCache;
import org.mspring.platform.config.entity.Config;
import org.mspring.platform.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 *
 * @since 2014年4月24日
 * @author Gao Youbo
 */
@Service
public class ConfigCacheService {
    private static final long CACHE_SECONDS = 60 * 60; // 一小时
    private static final String ALL_CONFIG_KEY = "__$$%^^)!_@#NM:__ALL__"; // 所有配置的缓存key
    private LruCache<String, Config> configCache = new LruCache<>(CACHE_SECONDS);
    private LruCache<String, Map<String, String>> allConfigCache = new LruCache<>(CACHE_SECONDS);

    public Config get(String name) {
        return configCache.get(name);
    }

    public void put(String name, Config config) {
        if (StringUtils.isBlank(name)) {
            return;
        }
        configCache.put(name, config);
    }

    public void remove(String name) {
        if (StringUtils.isBlank(name)) {
            return;
        }
        configCache.remove(name);
    }

    public void clear() {
        configCache.clear();
    }

    public void removeAllCache() {
        allConfigCache.clear();
    }

    public void putAllCache(Map<String, String> configs) {
        allConfigCache.put(ALL_CONFIG_KEY, configs);
    }

    public Map<String, String> getAllCache() {
        return allConfigCache.get(ALL_CONFIG_KEY);
    }
}
